
package com.ys.chatserver.im.util;

import com.eva.epc.common.util.CommonUtils;
import com.ys.chatserver.http.logic.LogicProcessor2;
import com.ys.chatserver.tool.EmptyUtils;

import java.util.List;
import java.util.Vector;
import java.util.regex.Pattern;

/**
 * IM服务器端的实用工具类。
 *
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public class ChatServerUtils {
    /**
     * 是否整数。
     *
     * @param str 整数字符串
     * @return 如果是合规的整数则返回true，否则返回false
     */
    public static boolean isInteger(String str) {
        if (CommonUtils.isStringEmpty(str, true))
            return false;

        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 获取用户类型
     *
     * @param uid
     * @return
     * @throws Exception
     */
    public static Integer getUserType(String uid) throws Exception {
        String sql = "select user_type from g_users where del_flag = 0 and user_uid = '" + uid + "';";
        Vector<Vector<?>> vectors = LogicProcessor2.db.queryData(sql);
        return Integer.parseInt(vectors.get(0).get(0).toString());
    }

    /**
     * 查询用户能看到的组织下的人员列表。
     *
     * @since 4.0
     */
    public static Vector<Vector<?>> getRosterByUid(String uid) throws Exception {
        Vector<Vector<?>> v = getNotifyUserByUid(uid);
        StringBuilder buf = new StringBuilder();
        if (!v.isEmpty()) {
            for (Vector<?> vector : v) {
                buf.append("'" + vector.get(0) + "',");
            }
            String sql = "select user_uid,user_mail from g_users where del_flag = 0 and user_type=0 and user_uid in " +
                    "(" + buf.substring(0, buf.length() - 1) + ")";
            return LogicProcessor2.db.queryData(
                    sql);
        } else {
            return v;
        }
    }

    /**
     * 获取所有我需要通知上下限的用户列表
     *
     * @param userUid
     * @return
     * @throws Exception
     */
    public static Vector<Vector<?>> getNotifyUserByUid(String userUid) throws Exception {
        String sql = "select account_type,user_type from g_users where del_flag = 0 and user_uid = '" + userUid + "';";
        Vector<Vector<?>> vectors = LogicProcessor2.db.queryData(sql);
        int accountType = Integer.parseInt(vectors.get(0).get(0).toString());
        int userType = Integer.parseInt(vectors.get(0).get(1).toString());// 0 用户  1设备
        if (userType == 1) { //设备推送给所有能看到设备的用户
            return ChatServerUtils.queryUserByDevId(userUid);
        } else { //所有能看到我的用户 同级和上级
            if (accountType == 1) {
                return ChatServerUtils.queryUserByUserId(userUid);
            }
        }
        return new Vector<>();
    }

    public static Vector<Vector<?>> queryUserByUserId(String userId) throws Exception {
        String sql = "select structure_level from structure_info " +
                "where structure_no = (select structure_no from structure_user_info " +
                "where user_id = '" + userId + "' and user_type='USER');";
        Vector<Vector<?>> vector = LogicProcessor2.db.queryData(sql);
        if (!vector.isEmpty()) {
            String structureLevel = vector.get(0).get(0).toString();
            List<String> listStringSplitValue = EmptyUtils.getListStringSplitValue(structureLevel);
            StringBuilder builder = new StringBuilder();
            for (String str : listStringSplitValue) {
                builder.append("'" + str + "',");
            }
            String sql1 = "select user_id from structure_user_info WHERE structure_no in (" + builder.substring(0, builder.length() - 1) + ") and user_type='USER' and user_id!='" + userId + "'";
            return LogicProcessor2.db.queryData(sql1);
        }
        return new Vector<>();

    }

    public static Vector<Vector<?>> queryUserByDevId(String devId) throws Exception {
        String sql = "select structure_level from structure_info WHERE structure_no=(select structure_no from my_structure WHERE dev_id='" + devId + "'  and is_finish=0)";
        Vector<Vector<?>> vector = LogicProcessor2.db.queryData(sql);
        if (!vector.isEmpty()) {
            String structureLevel = vector.get(0).get(0).toString();
            List<String> listStringSplitValue = EmptyUtils.getListStringSplitValue(structureLevel);
            StringBuilder builder = new StringBuilder();
            for (String str : listStringSplitValue) {
                builder.append("'" + str + "',");
            }
            String sql1 = "select user_id from structure_user_info WHERE structure_no in (" + builder.substring(0, builder.length() - 1) + ") and user_type='USER' and user_id!='" + devId + "'";
            return LogicProcessor2.db.queryData(sql1);
        }
        return new Vector<>();
    }

//	/**
//	 * 查询用户的好友列表。
//	 * 
//	 * @param uidOrMail
//	 * @return
//	 * @throws Exception
//	 * @deprecated since 4.0, 因为性能问题而不建议使用本方法
//	 */
//	public static Vector<Vector> getRosterByUidOrMail(String uidOrMail) throws Exception
//	{
//		return LogicProcessor2.db.queryData(
//				"select a.friend_user_uid,c.user_mail,a.user_uid "+
//						"	from chat_my_firends a "+
//						"	left join g_users b on a.user_uid = b.user_uid "+
//						"	left join g_users c on a.friend_user_uid = c.user_uid "+
//						// userName是登陆时用的，它可能是uid也可能是email
////						"	where (ltrim(str(a.user_uid))='"+uidOrMail+"' or b.user_mail='"+uidOrMail+"')");
//						"	where "+(CommonUtils.isEmail(uidOrMail)?"user_mail='"+uidOrMail+"'":"user_uid="+uidOrMail));
//	}
//
//	public static void main(String[] args) {
//		System.out.println("ChatServerUtils="+ChatServerUtils.isInteger("10000"));
//	}
}
